﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ModelViewer
{
    public class MenuRenderer : ToolStripProfessionalRenderer
    {
        private Color menuBackground = Color.FromArgb(100, 100, 100);
        private Color treeViewerBackground = Color.FromArgb(64, 64, 64);


        private Brush selectedBackground = new SolidBrush(Color.FromArgb(56, 56, 56));

        private Brush pressedBackground = new SolidBrush(Color.FromArgb(68, 68, 68));

        /// <summary>
        /// Set true for the menu we use on the treeviewer
        /// </summary>
        private bool treeViewer = false;

        public bool TreeViewer
        {
            get { return treeViewer; }
            set { treeViewer = value; }
        }

       

        public MenuRenderer(ProfessionalColorTable ct) : base(ct)
        {
        }

        protected override void InitializeItem(ToolStripItem item)
        {
            base.InitializeItem(item);

            item.ForeColor = Color.White;

            if (item is ToolStripMenuItem)
            {
                foreach (ToolStripItem child in ((ToolStripMenuItem)item).DropDownItems)
                {
                    child.ForeColor = Color.White;
                }
            }
        }

        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            Rectangle rect = e.AffectedBounds;

            if (treeViewer)
            {
                // Drop down items are solid
                Brush brush = new SolidBrush(treeViewerBackground);

                e.Graphics.FillRectangle(brush, rect);
            }
            else if (e.ToolStrip.IsDropDown)
            {
                // Drop down items are solid
                Brush brush = new SolidBrush(menuBackground);

                e.Graphics.FillRectangle(brush, rect);
            }
            else
            {
                // Top level is gradient
                Brush brush = new LinearGradientBrush(rect.Location, new Point(rect.Left, rect.Bottom), ColorTable.MenuStripGradientBegin, ColorTable.MenuStripGradientEnd);

                e.Graphics.FillRectangle(brush, rect);
            }

        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs myMenu)
        {
            ToolStripItem item = myMenu.Item;
            Rectangle menuRectangle = new Rectangle(Point.Empty, item.Size);

            // HasDropDownItems
            // IsOnDropDown
            // Pressed
            // 

            if (item.Selected)
            {
                //Fill Color
                myMenu.Graphics.FillRectangle(selectedBackground, menuRectangle);

                // Border Color
                myMenu.Graphics.DrawRectangle(Pens.Black, 1, 0, menuRectangle.Width - 2, menuRectangle.Height - 1);
            }
            else if (item.Pressed)
            {
                myMenu.Graphics.FillRectangle(pressedBackground, menuRectangle);
                // myMenu.Graphics.DrawRectangle(Pens.Red, 1, 0, menuRectangle.Width - 2, menuRectangle.Height - 1);
            }
            else if (myMenu.Item.IsOnDropDown)
            {
                //Fill Color
                Brush brush = new SolidBrush(menuBackground);
                myMenu.Graphics.FillRectangle(brush, menuRectangle);
            }
            else
            {
                base.OnRenderMenuItemBackground(myMenu);
            }
        }

        protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs menu)
        {
            ToolStripItem item = menu.Item;
            Rectangle menuRectangle = new Rectangle(Point.Empty, item.Size);
            Brush brush = new SolidBrush(menuBackground); 
            menu.Graphics.FillRectangle(brush, menuRectangle);
            int middle = (menuRectangle.Top + menuRectangle.Bottom) / 2;
            menu.Graphics.DrawLine(Pens.LightGray, menuRectangle.Left + 5, middle - 1, menuRectangle.Right - 5, middle - 1);
            menu.Graphics.DrawLine(Pens.Black, menuRectangle.Left + 5, middle, menuRectangle.Right - 5, middle);
        }
    }
}
